package com.guigu.controller.design;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.mapper.design.DModuleDetailsMapper;
import com.guigu.pojo.design.DFile;
import com.guigu.pojo.design.DModule;
import com.guigu.pojo.design.DModuleDetails;
import com.guigu.service.design.DFileService;
import com.guigu.service.design.DModuleDetailsService;
import com.guigu.service.design.DModuleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mr.Zhang
 * @since 2024-12-06
 */
@Controller
@RequestMapping("/d-module-details")
public class DModuleDetailsController {

    @Autowired
    private DModuleService dModuleService;

    @Autowired
    private DModuleDetailsService dModuleDetailsService;

    @Autowired
    private DFileService dFileService;

    @GetMapping("/initialMaterialApprovalModal")
    public String initialMaterialApprovalModal(){
        return "jsp/design/Review_Material_Composition_Design";
    }

    @GetMapping("/ItemApprovalTable")
    @ResponseBody
    public DModule ItemApprovalTable(HttpServletRequest req){
        Integer id = StringUtils.isNotBlank(req.getParameter("id")) ? Integer.parseInt(req.getParameter("id")) : null;
        QueryWrapper<DModule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        DModule dModule = dModuleService.getOne(queryWrapper);
        return dModule;
    }

    @GetMapping("/initialTemplate")
    @ResponseBody
    public ArrayList<DModuleDetails> initialTemplate(HttpServletRequest req){
        Integer id = StringUtils.isNotBlank(req.getParameter("id")) ? Integer.parseInt(req.getParameter("id")) : null;
        ArrayList<DModuleDetails> dModuleDetails = dModuleDetailsService.selectArrByParentId(id);
        return dModuleDetails;
    }

    @PostMapping("/Audit")
    @ResponseBody
    public Boolean Audit(@RequestBody HashMap<String,Object> map){
        Integer id = Integer.parseInt(map.get("id").toString());
        String checkTag = map.get("checkTag").toString();
        String checker = map.get("checker").toString();
        DModule dModule = new DModule();
        dModule.setId(id);
        dModule.setCheckTag(checkTag);
        dModule.setChecker(checker);
        dModule.setCheckTime(new Date());

        boolean b = dModuleService.updateById(dModule);
        return b;
    }

    @GetMapping("/initialProcessMaterial")
    @ResponseBody
    public ArrayList<DModuleDetails> initialProcessMaterial(HttpServletRequest req){
        String productId = StringUtils.isNotBlank(req.getParameter("productId")) ? req.getParameter("productId") : null;
        QueryWrapper<DModule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PRODUCT_ID", productId);
        DModule dModule = dModuleService.getOne(queryWrapper);
        if (dModule != null){
            ArrayList<DModuleDetails> dModuleDetails = dModuleDetailsService.selectArrByParentId(dModule.getId());
            return dModuleDetails;
        }
        return null;
    }

}

